class_name CampManager
extends Node2D

## camp改变可能还涉及到LayerMask的改变
## camp直接绑定全局Group
## 可能会多个阵营

#region 属性
@export var entity:Entity
## 是否不改变group
@export var is_ignore_group:bool
@export var camp_info_lists:Array[CampInfoList]
var connect_obj_dict:Dictionary = {}
#endregion
#region 字段
#endregion
#region 信号
#endregion

#region 基类方法
#endregion
#region 公共方法
func init(args:Dictionary = {}):
	connect_obj_dict[entity.entity_data.data_changed] = ConnectObj.new(entity.entity_data.data_changed,[_on_data_changed])
	var target_camp = entity.creater.entity_data.get_attribute_value("Camp") if entity.creater else null
	change_camp(entity.entity_data.default_camp if target_camp == null else target_camp)
func change_camp(target_camp:GameEnum.Camp) -> bool:
	var camp = entity.entity_data.get_attribute_value("Camp")
	if camp == target_camp:
		return false
	else:
		entity.entity_data.find_attribute("Camp").force_set_base_value(target_camp)
		## 改变layer
		for camp_info_list in camp_info_lists:
			for path in camp_info_list.area_paths:
				var area:Area2D = get_node(path)
				var info:CampAreaInfo = camp_info_list.camp_info_dict.get(target_camp) as CampAreaInfo
				var default_camp_info:CampInfo = GameCampSystem.get_camp_info(target_camp)
				area.collision_layer = info.layer if info != null else default_camp_info.layer
				area.collision_mask = info.mask if info != null else default_camp_info.mask
		return true
#endregion
#region 私有方法
func _on_data_changed(tag_space:String,tag:String,old_value,new_value):
	if tag_space == "attribute" and tag == "Camp":
		if is_ignore_group:
			return
		GameCampSystem.group_remove(old_value,entity.root)
		GameCampSystem.group_add(new_value,entity.root)
#endregion
#region 生命周期函数
func _notification(what: int) -> void:
	if what == NOTIFICATION_PREDELETE:
		for connect_obj:ConnectObj in connect_obj_dict.values():
			if connect_obj:
				connect_obj.disconnect_all()
#endregion
